define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/qpay/orderInfo.html",
    "text!templates/qpay/debitCard.html",
    "text!templates/qpay/creaditCard.html",
    "wTools"

], function ($, _, Backbone, orderInfo, debit, creadit) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        isEmpty: true,
        bPass: false,
        templateOrderInfo: _.template(orderInfo),
        templateDebit: _.template(debit),
        templateCreadit: _.template(creadit),
        events: {
            "blur #username":"nameTest",
            "blur #idCard":"idCardTest",
            "blur #phone":"phoneNumTest",
            "blur #iardCvn2":"cvv2NumTest",
            "blur #iardExprDt":"dateNumTest",
            "input #username":"limitLen",
            "input #idCard":"limitLen",
            "input #phone":"limitLen",
            "input #iardCvn2":"limitLen",
            "input #iardExprDt":"limitLen",
            "input #verifyNo":"limitLen",
            "tap #getVerify": "getVerify",
            "tap #confirm": "confirm"
        },
        initialize: function () {

            $("#head_back").show();

            if (wData.urlData.token != undefined && wData.urlData.token != "" && wData.urlData.token != null && wData.urlData.token != wData.token) {
                wData.token = wData.urlData.token;
            }

            var postData = {
                "token": wData.token
            }, that = this;
            if(wData.cardInfo){
                wAjaxForm('getTokenInfo', '' ,postData, function(result){
                    wData.isBindCard = result.tokenInfo.isBindCard;
                    wData.isUpload = result.tokenInfo.isUpload;

                    wAjaxForm("queryVoucherInfo", "", postData, function (rep) {
                        rep = JSON.parse(rep);
                        if (rep.status == 'S') {
                            wData.orderInfo = {
                                trxAmt:rep.trxAmt,
                                mrchntName:rep.mrchntName,
                                ordrName:rep.ordrName,
                                oriTrxId:result.tokenInfo.trxId
                                
                            };
                            
                            $("#home").append(that.templateOrderInfo(wData.orderInfo));

                            if(wData.isBindCard == '0' && wData.isUpload == '0' ){
                    
                                if(wData.cardInfo.cardType == 'CC'){
                                    that.$el.append(that.templateCreadit(wData.cardInfo))
                                }
                                else if(wData.cardInfo.cardType == 'DC'){
                                    that.$el.append(that.templateDebit(wData.cardInfo))
                                }
                            }
                        }else{
                            wTips("请重试");
                        }
                    });
                },function(err) {
                    wData.errorMessage = "操作超时,请重试";
                    location.hash = "fail";
                })
            }
            else{
                location.hash = 'qPay'
            }
        },
        //获取验证码
        getVerify: function () {
            if(this.bPass){
                return;
            }
            var bOK;
            if(wData.cardInfo.cardType == 'CC'||wData.cardInfo.cardType == '00'){
                bOK = $('#getVerify').hasClass('disabled' ) || !this.nameTest() || !this.phoneNumTest() || !this.idCardTest() || !this.cvv2NumTest() || !this.dateNumTest();
            }
            else if(wData.cardInfo.cardType == 'DC'||wData.cardInfo.cardType == '01'){
                bOK = $('#getVerify').hasClass('disabled' ) || !this.nameTest() || !this.phoneNumTest() || !this.idCardTest();
            }
            if(bOK){
                return;
            }
            this.bPass = true;
            var postData={};
            var that = this;
            if(wData.isBindCard == '0' && wData.isUpload == '0'){
                if(wData.cardInfo.cardType == 'CC' ||wData.cardInfo.cardType =='00' ){
                    postData.bkAcctNo = $('#bankCode').html().replace(/\s/g, '');
                    postData.cstmrNm = $('#username').val();
                    postData.iDNo = $('#idCard').val();
                    postData.mobNo = $('#phone').val();
                    postData.bankName = $('#cardName').html().replace(/\s/g, '');
                    postData.bkAcctTp = '00';
                    postData.iardCvn2 = $('#iardCvn2').val();
                    postData.iardExprDt = $('#iardExprDt').val().substring(0,2)+'/'+$('#iardExprDt').val().substring(2);
                    postData.token = wData.token;
                }
                else if(wData.cardInfo.cardType == 'DC'||wData.cardInfo.cardType== '01'){
                    postData.bkAcctNo = $('#bankCode').html().replace(/\s/g, '');
                    postData.cstmrNm = $('#username').val();
                    postData.iDNo =$('#idCard').val();
                    postData.mobNo = $('#phone').val();
                    postData.bankName = $('#cardName').html().replace(/\s/g, '');
                    postData.bkAcctTp = '01';
                    postData.token = wData.token;
                }
            }
            var _that = this;
            wAjaxForm('quickPayOrder', '', postData, function(result) {
                result = JSON.parse(result);
                _that.bPass = false;
                if(result.bizStsCd == '1'){
                    $('#getVerify').addClass('disabled');
                    that.setTimer();
                }else if(result.bizStsCd == '0'){
                    wTips(result.sysRtnDesc);
                }
                    
            })
        },
        confirm: function() {
            if(this.bPass){
                return;
            }
            var bOK;
            if(wData.cardInfo.cardType == 'CC'||wData.cardInfo.cardType == '00'){
                bOK = $('#confirm').hasClass('disabled') || this.isEmpty() || !this.nameTest() || !this.phoneNumTest() || !this.idCardTest() || !this.cvv2NumTest() || !this.dateNumTest();
            }
            else if(wData.cardInfo.cardType == 'DC'||wData.cardInfo.cardType == '01'){
                bOK = $('#confirm').hasClass('disabled' ) || this.isEmpty() || !this.nameTest() || !this.phoneNumTest() || !this.idCardTest();
            }
            if(bOK){
                return;
            }
            this.bPass = true;
            var postData = {
                token: wData.token,
                smsCode: $('#verifyNo').val()
            }
            var _that = this;
            wAjaxForm('quickPayConfirm', '' ,postData, function(result) {
                _that.bPass = false;
                result = JSON.parse(result);
                if(result.status == 'S'){
                    wData.tip = {
                        img: '<img src="images/icon_success2.png">',
                        text: '您好，您已成功付款<br />￥'+wData.orderInfo.trxAmt
                    }
                    wData.status = 'S';
                }else if(result.status == 'P'){
                    wData.tip = {
                        img: '<img src="images/icon_fail.png">',
                        text: '处理中，请稍候查询'
                    }
                    wData.status = 'P';
                }else if(result.status == 'F'){
                    wTips(result.sysRtnDesc)
                    return;
                }
                location.hash = 'qPayResult';
            })
        },
        //银行卡号校验
        bankTest: function() {
            var data = $('#bankCode').val();
            var result = this.luhmCheck(data);
            $('#bankCode').parents('li').find('.tip').html(result.error)
            return result.success
        },
        //展开订单信息
        more: function () {
            if ($("#more").html() == "收起") {
                $("#order").css({"height": "2.6rem"});
                $("#more").html("更多");
            }
        },
        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            $('#home').html('');
            $("#head_back").show();
            if (this.Interval != -1) {
                clearInterval(this.Interval);
                this.Interval = -1;
                $("#getVerify").html("免费获取").removeClass("color_9C9B9B").addClass("color_63b4ff");
            }
            delete this;
        },
        nameTest: function(event) {
            var data = $('#username').val();
            var result = wCheck(data,'username');
            $('#username').parents('li').find('.tip').html(result.error)
            return result.success
        },
        idCardTest: function() {
            var data = $('#idCard').val();
            var result = wCheck(data,'idCard');
            $('#idCard').parents('li').find('.tip').html(result.error)
            return result.success
        },
        phoneNumTest: function() {
            var data = $('#phone').val();
            var result =wCheck(data,'mobilePhone');
            $('#phone').parents('li').find('.tip').html(result.error)
            return result.success;
        },
        cvv2NumTest: function() {
            var data = $('#iardCvn2').val();
            var result = wCheck(data,'cvv2');
            $('#iardCvn2').parents('li').find('.tip').html(result.error)
            return result.success
        },
        dateNumTest: function() {
            var data = $('#iardExprDt').val();
            var result = wCheck(data,'dateNum');
            $('#iardExprDt').parents('li').find('.tip').html(result.error)
            return result.success;
        },
        isEmpty: function(event) {
            var _el = $('.listInfo').find('input');
            for(var i = 0; i<_el.size();i++){
                if(_el.eq(i).val()==''){
                    this.$isEmpty = true;
                    break;
                }else{
                    this.$isEmpty = false;
                }
            }
            if(!this.$isEmpty){
                $('#next').removeClass('disabled');
                $('#confirm') && $('#confirm').removeClass('disabled');
            }
            else{
                $('#next').addClass('disabled');
                $('#confirm') && $('#confirm').addClass('disabled');
            }
            return this.$isEmpty;
        },
        limitLen: function(event) {
            var lengths = 0;
            var id = event.target.id
            if(id == 'bankCode'){
                lengths = 19
                nonNumeric($('#'+id));
            }
            else if(id == 'username'){
                lengths = 20;
                limitName($('#'+id),lengths);
                this.isEmpty();
                return;
            }
            else if(id == 'verifyNo'){
                lengths = 6;
            }else if(id == 'idCard'){
                lengths = 18;
            }
            else if(id == 'iardExprDt'){
                lengths = 4;
            }
            else if( id == 'iardCvn2'){
                lengths = 3;
            }
            else if( id == 'phone'){
                lengths = 11;
            }
            else if( id== 'verifyNo'){
                lengths = 6;
            }
            limitLength($('#'+id),lengths);
            this.isEmpty();
        },
        back: function () {
            history.back();
        },
        setTimer: function() {
            var sec = 60;
            var str = '';
            var timer = setInterval(function() {
                sec--;
                if(sec==0){
                    clearInterval(timer);
                    str = '获取验证码';
                    $('#getVerify').removeClass('disabled');
                }else{
                    str = sec+'s重新获取';
                }
                $('#getVerify').html(str);
            },1000);
        },
    });
});